package com.project.springboot.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileNotFoundException;

/**
 * Description：数据访问层
 * Created by 朱正磊 on 2019-03-11 23:19
 */
@Repository
public class ProductDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void addProduct(String pname) {
        String sql = "INSERT INTO product (pname) VALUES ('"+ pname +"')";
        jdbcTemplate.execute(sql);
    }

    @Transactional(noRollbackFor = NullPointerException.class)
    public void addProductBatch(String... pnames) throws Exception {
        for (String pname : pnames) {
            String sql = "INSERT INTO product (pname) VALUES ('"+ pname +"')";
            jdbcTemplate.execute(sql);
            if (true) {
                //throw new NullPointerException();
            }
        }
    }

}
